//
//  RMMainViewController.m
//  RMNote
//
//  Created by lxf on 16/4/14.
//  Copyright © 2016年 lxf. All rights reserved.
//

#import "RMMainViewController.h"
#import "RMMainCenterView.h"
#import "RMDateTableView.h"
#import "RMBrowseShortNoteViewController.h"
#import "RMNVTitleView.h"
#import "NCAnimationView.h"
#import "RMWriteShortNoteViewController.h"
#import "RMSurpriseViewController.h"
#import "ObjectFunction.h"
#import "UIImage+ImageEffects.h"
#import "RMBlurryView.h"
#import "RMWriteNoteViewController.h"
#import "RMDateBaseManager.h"
#import "RMTimeHandle.h"
#import "RMBaseModel.h"
#import "RMNoteTableView.h"
#import "RMSecretController.h"


@interface RMMainViewController ()<UIScrollViewDelegate,NCAnimationViewDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) RMNVTitleView *titleView;
@property (nonatomic, strong) RMBrowseShortNoteViewController *browVC;
@property (nonatomic , strong) RMSurpriseViewController *surpriseVC;
@property (nonatomic, strong) RMBlurryView *blurryView;
@property (nonatomic, strong) NCAnimationView *menu;
@property (nonatomic, strong) RMMainCenterView *mainCenterView;

@end


@implementation RMMainViewController

@synthesize browVC;
@synthesize surpriseVC;

- (void)viewWillAppear:(BOOL)animated{
    [self.mainCenterView reloadDataForNoteTableView];
    [self.mainCenterView reloadDataForDateTableView];

}

- (void)viewDidLoad {
    [super viewDidLoad];

    
//    [self createData];
    
    // 初始化界面
    [self createMainView];
    
    // 1.添加时间轴页面
    self.mainCenterView = [[RMMainCenterView alloc]initWithFrame:CGRectMake(kScreenWidth, 0, kScreenWidth, kScreenHeight - kTopHeight)];
    

    __weak RMMainViewController *MV = self;
    self.mainCenterView.pushNoteViewBlock = ^(RMBaseModel *model){
        RMWriteNoteViewController *rv = [[RMWriteNoteViewController alloc]init];
        rv.baseModel = model;
        [MV.navigationController pushViewController:rv animated:YES];
    };
    [self.scrollView addSubview:self.mainCenterView];
    // 2.添加便签页面

    browVC = [[RMBrowseShortNoteViewController alloc]init];
    [self addChildViewController:browVC];
    browVC.view.frame = CGRectMake(0,0, kScreenWidth, kScreenHeight-kTopHeight);
    [self.scrollView addSubview:browVC.view];
    //添加笑话页面
    surpriseVC = [[RMSurpriseViewController alloc]init];
    [self addChildViewController:surpriseVC];
    surpriseVC.view.frame = CGRectMake(2*kScreenWidth, 0, kScreenWidth, kScreenHeight);
//    [self.scrollView addSubview:surpriseVC.view];
    
    // 3. 初始化 BtnView
    [self createAnimationBtnView];
    

    
}

- (void)viewDidAppear:(BOOL)animated{
    // 4.判断锁屏状态
    if ([self getLockStatuse]) {
        
        [self addBurryPicture];
        self.scrollView.scrollEnabled = NO;
    }else{
        self.scrollView.scrollEnabled = YES;
    }
}


- (void)createMainView{
    self.titleView = [[[NSBundle mainBundle]loadNibNamed:@"RMNVTitleView" owner:nil options:nil] lastObject];;
    //    self.navigationItem.title = @"a";
    
    self.navigationItem.titleView = self.titleView;
    
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    self.scrollView.bounces = NO;
    self.scrollView.contentSize = CGSizeMake(2*kScreenWidth, kScreenHeight - kTopHeight );
    self.scrollView.contentOffset = CGPointMake(kScreenWidth, 0);
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.delegate = self;
    [self.view addSubview:self.scrollView];
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}

- (void)createAnimationBtnView{
    UIImage *bgImage = [UIImage imageNamed:@"btnBg"];
    NCAnimationViewItem *noteItem = [[NCAnimationViewItem alloc] initWithImage:bgImage highlightedImage:bgImage ContentImage:[UIImage imageNamed:@"notehiglite"] highlightedContentImage:nil];

    // People MenuItem.
    NCAnimationViewItem *shotNoteItem = [[NCAnimationViewItem alloc] initWithImage:bgImage highlightedImage:bgImage ContentImage:[UIImage imageNamed:@"shortNoteHiglith"] highlightedContentImage:nil];
    NSArray *menus = [NSArray arrayWithObjects:noteItem, shotNoteItem, nil];
    self.menu = [NCAnimationView viewWithFrame:CGRectMake(kScreenWidth, kScreenHeight - kTopHeight - 167, 100, 167) viewArray:menus];
    self.menu.userInteractionEnabled = YES;
    self.menu.delegate = self;
    [self.scrollView addSubview:self.menu];
}




#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [self changeTitleAndPageColor];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    [self changeTitleAndPageColor];    
}

- (void)changeTitleAndPageColor{
    if (self.scrollView.contentOffset.x >= kScreenWidth * 0.5 && self.scrollView.contentOffset.x <= kScreenWidth * 1.5) {
        self.titleView.leftImageView.backgroundColor = [UIColor whiteColor];
        self.titleView.centerImageView.backgroundColor = kRMMainLineColor;
        self.titleView.rightImageView.backgroundColor = [UIColor whiteColor];
        self.titleView.titleLabel.text = @"日记";
        
        UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@""] style:(UIBarButtonItemStylePlain) target:self action:@selector(lockAction:)];
        self.navigationItem.rightBarButtonItem = nil;
        if ([self getLockStatuse]) {
            rightItem.image = [UIImage imageNamed:@"lock_close"];
        }else{
            rightItem.image = [UIImage imageNamed:@"lock_open"];
        }
        self.navigationItem.rightBarButtonItem = rightItem;

        
        
    }else if (self.scrollView.contentOffset.x >= kScreenWidth * 1.5){
        self.titleView.leftImageView.backgroundColor = [UIColor whiteColor];
        self.titleView.centerImageView.backgroundColor = [UIColor whiteColor];
        self.titleView.rightImageView.backgroundColor = kRMMainLineColor;
        self.titleView.titleLabel.text = @"笑话";
    }else{
        self.titleView.leftImageView.backgroundColor = kRMMainLineColor;
        self.titleView.centerImageView.backgroundColor = [UIColor whiteColor];
        self.titleView.rightImageView.backgroundColor = [UIColor whiteColor];
        self.titleView.titleLabel.text = @"便签";

        self.navigationItem.rightBarButtonItem = nil;
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"添加" style:(UIBarButtonItemStyleDone) target:self action:@selector(editAction:)];

    }
}

// 日记页面rightBarButtonItem 锁 的点击事件
- (void)lockAction:(UIBarButtonItem *)sender{
    if ([self getLockStatuse]) {

        RMSecretController *rc = [[RMSecretController alloc]init];
        rc.passBlock = ^(){
            sender.image = [UIImage imageNamed:@"lock_open"];
            [self.blurryView removeFromSuperview];
            self.blurryView = nil;
            self.mainCenterView.userInteractionEnabled = YES;
            [self saveLockStatuse:NO];
            self.scrollView.scrollEnabled = YES;
        };
        [self presentViewController:rc animated:YES completion:nil];
    }else{
        if ([[NSUserDefaults standardUserDefaults]valueForKey:@"passWord"]==nil) {
            RMSecretController *rc = [[RMSecretController alloc]init];
            [self presentViewController:rc animated:YES completion:^{
                
            }];
            return;
        }
        
        sender.image = [UIImage imageNamed:@"lock_close"];
        self.mainCenterView.userInteractionEnabled = NO;
        self.scrollView.scrollEnabled = NO;
        [self addBurryPicture];
        [self saveLockStatuse:YES];
    }
    
}

// 便签页面rightBarButtonItem 添加 的点击事件
- (void)editAction:(id)sender{
    RMWriteShortNoteViewController *RWSN = [[RMWriteShortNoteViewController alloc]init];
    [self.navigationController pushViewController:RWSN animated:YES];
}

// 添加蒙版 view
- (void)addBurryPicture{
    for (UIView *view in self.scrollView.subviews) {
        if ([view isKindOfClass:[RMBlurryView class]]) {
            return;
        }
    }
//    if (!self.blurryView) {
//        return;
//    }
    UIGraphicsBeginImageContext(self.view.bounds.size);     //currentView 当前的view  创建一个基于位图的图形上下文并指定大小为
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];//renderInContext呈现接受者及其子范围到指定的上下文
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();//返回一个基于当前图形上下文的图片
    UIGraphicsEndImageContext();//移除栈顶的基于当前位图的图形上下文
    
    self.blurryView = [[RMBlurryView alloc]initWithFrame:CGRectMake(kScreenWidth, 0, kScreenWidth, 0)];
    self.blurryView.backgroundColor = [UIColor colorWithWhite:0.17 alpha:0.53];
    [self.scrollView insertSubview:self.blurryView belowSubview:self.menu];
    [UIView animateWithDuration:0.9 animations:^{
        self.blurryView.frame = CGRectMake(kScreenWidth, 0, kScreenWidth, kScreenHeight - 64);
        
    } completion:^(BOOL finished) {
        self.blurryView.image = [viewImage applyDarkEffect];
        self.blurryView.backgroundColor = [UIColor clearColor];
        
    }];
}

// 保存锁的状态到 userdefaults
- (void)saveLockStatuse:(BOOL)islock{
    [[NSUserDefaults standardUserDefaults]setValue:[NSNumber numberWithBool:islock] forKey:@"isLock"];
}

// 取出锁的状态 从 userdefaults
- (BOOL)getLockStatuse{
   NSNumber *num = [[NSUserDefaults standardUserDefaults]valueForKey:@"isLock"];
    BOOL islock = num.boolValue;
    return islock;
}

- (void)animationView:(NCAnimationView *)view didSelectedIndex:(NSInteger)index{
    RMWriteNoteViewController *rv = [[RMWriteNoteViewController alloc]init];
    RMWriteShortNoteViewController *rnv = [[RMWriteShortNoteViewController alloc]init];
    switch (index) {
        case 0:
            [self.navigationController pushViewController:rv animated:YES];

            break;
        case 1:
            [self.navigationController pushViewController:rnv animated:YES];
            
        default:
            break;
    }
    
    NSLog(@"点击了第%ld个button",index);
}
//  模拟数据插入数据库
- (NSArray *)createData{
    
   
    NSMutableArray *arr = [NSMutableArray array];
     [[RMDateBaseManager defaultManager] createTable:kRMNoteTable];
    for (int i = 0; i < 3; i++) {
        int year = arc4random_uniform(20)+1990;
        int moth = arc4random_uniform(12)+1;
        int day = arc4random_uniform(27)+1;
        int hour = arc4random_uniform(23)+1;
        int minute = arc4random_uniform(59)+1;
        int second = arc4random_uniform(59) + 1;
        
        
        NSString *str = [NSString stringWithFormat:@"%d%02d%02d%02d%02d%02d",year,moth,day,hour,minute,second];
        
        NSDate *date = [[RMTimeHandle shareInstance] dateForDateStr:str andFormatter:@"yyyyMMddHHmmss"];
        RMBaseModel *model = [[RMBaseModel alloc]init];
        
//        model.content = 
        
        model.title = @"如果~有如果";
        model.createDate = date;
        
        BOOL result = [[RMDateBaseManager defaultManager] insertIntoTable:kRMNoteTable createDate:date Model:model];
    }
//
    return arr;
}





@end
